﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Management;
using Log4netExpand;

namespace Log4netExpand
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            log4net.ILog log = log4net.LogManager.GetLogger("myLogger");

            LogContent logmodel = new LogContent();
            logmodel.Event_Type = 3;
            logmodel.EventCategory = "登陆系统";
            logmodel.Event_ID = 1;
            logmodel.ComputerName = Request.UserHostAddress;
            logmodel.Mac_Address = GetMacAddress();
            logmodel.Source = "SS";
            logmodel.SourceType = "1";
            logmodel.UserName = "ADMIN";
            logmodel.Description = "TEST";//在记录数据库操作时，可以在petapoco底层将sql语句放入其中。          
            LogHelper.WriteLog(logmodel);

            lbMsg.Text = "数据记录成功！";
        }

        public string GetMacAddress()
        {
            string mac = "";
            try
            {
                //获取网卡硬件地址 
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();

                List<string> list = new List<string>();

                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();

                        list.Add(mac);
                    }
                }
                moc = null;
                mc = null;

                mac = list[list.Count - 1];

                return mac;
            }
            catch
            {
                return "unknow";
            }
        }
    }
}